package linkwechat.controller.controllersdk;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import linkwechat.pojo.ReturnData;
import linkwechat.pojo.SysUser;
import linkwechat.sysservice.SysUserService;
import linkwechat.util.SendMessage;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static linkwechat.pojo.ReturnEnum.ERROR_REGISTERED;


@RestController
@RequestMapping("sdkssm")
public class Sdk_SSM_Controller {

    @Resource
    SysUserService userService;

    //    手机号查询
    @RequestMapping("selectPhone")
    public ReturnData selectPhone(String phone) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        List<SysUser> list = userService.list(wrapper.eq("phonenumber", phone));
        if (list.size() != 0) {
            return ReturnData.setResp(ERROR_REGISTERED, "已注册");
        }
        return ReturnData.respSuccess("");
    }


    //    短信发送
    @RequestMapping("sendTest")
    public ReturnData Send(String phone) {
        SendMessage.sendMsg(phone);
        return ReturnData.respSuccess(SendMessage.verCode);
    }

    //    用户注册
    @RequestMapping("userRegister")
    public ReturnData selectPhone(SysUser user) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        int i = userService.registerUser(user);
//        boolean save = userService.save(user);
        return ReturnData.respSuccess(i);
    }


}
